package com.lmm.videopager_java;

import android.graphics.Color;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.viewpager2.adapter.FragmentStateAdapter;

import com.google.android.material.tabs.TabLayout;
import com.google.android.material.tabs.TabLayoutMediator;
import com.lmm.videopager_java.databinding.ActivityMainBinding;

public class MainActivity extends AppCompatActivity {

    private ActivityMainBinding binding;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = ActivityMainBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
        binding.mainViewPager.setAdapter(new FragmentStateAdapter(this) {
            @NonNull
            @Override
            public Fragment createFragment(int position) {
                if (position != 1) {
                    return new FooFragment();

                }
                return new VideoFragment();
            }

            @Override
            public int getItemCount() {
                return 3;
            }
        });
        binding.mainViewPager.setCurrentItem(1, false);
        binding.tabLayout.setSelectedTabIndicatorColor(Color.BLACK);
        new TabLayoutMediator(binding.tabLayout, binding.mainViewPager, new TabLayoutMediator.TabConfigurationStrategy() {
            @Override
            public void onConfigureTab(@NonNull TabLayout.Tab tab, int position) {
                tab.setText("foo");
                if (position == 1) {
                    tab.setText("video");
                }
            }
        }).attach();
    }
}